import { PureComponent } from 'react';

export default class CodeCell extends PureComponent {
  static defaultProps = {
    onRender() {},
  };

  cell = null;

  componentDidMount() {
    this.notifyRender();
  }

  componentDidUpdate(prevProps) {
    if (prevProps.text !== this.props.text || prevProps.html !== this.props.html) {
      this.notifyRender();
    }
  }

  notifyRender() {
    this.props.onRender(this.cell);
  }

  render() {
    /* eslint-disable no-unused-vars */
    const { text, html, onRender, ...props } = this.props;
    /* eslint-enable no-unused-vars */
    const ref = (cell) => {
      this.cell = cell;
    };

    if (html) {
      const curHtml = html + `<span class="diff-code-comment-icon"></span>`;
      return <td ref={ref} {...props} dangerouslySetInnerHTML={{ __html: curHtml }} />;
    }
    // 伪元素添加点击事件不好加，直接在增加一个子元素
    return (
      <td ref={ref} {...props}>
        {text}
        <span className="diff-code-comment-icon" />
      </td>
    );
  }
}
